<?php

/**
 * This is the model class for table "{{contact}}".
 *
 * The followings are the available columns in table '{{contact}}':
 * @property integer $id
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $phone
 * @property string $title
 * @property string $message
 * @property integer $date_created
 * @property integer $status
 */
class Contact extends CActiveRecord
{

    public $verifyCode;

    const STATUS_NEW  = 1;
    const STATUS_DONE = 2;

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{contact}}';
    }

    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class'           => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'date_created',
            )
        );
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email ,phone, title, message, verifyCode', 'required', 'on' => 'standard-contact'),
            array('email', 'email'),
            //array('status', 'numerical', 'integerOnly' => true),
            array('first_name, last_name, email, phone', 'length', 'max' => 100),
            array('title', 'length', 'max' => 255),
            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'on' => 'standard-contact'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, first_name, last_name, email, phone, title, message, date_created, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id'           => 'ID',
            'first_name'   => Yii::t('main', 'First Name'),
            'last_name'    => Yii::t('main', 'Last Name'),
            'email'        => Yii::t('main', 'Email'),
            'phone'        => Yii::t('main', 'Phone'),
            'title'        => Yii::t('main', 'Title'),
            'message'      => Yii::t('main', 'Message'),
            'date_created' => Yii::t('main', 'Submitted time'),
            'status'       => Yii::t('main', 'Status'),
            'verifyCode'   => Yii::t('main', 'Verification Code'),
        );
    }

    public function getStatusLabel()
    {
        return $this->status == Contact::STATUS_DONE ? '<span class="label label-success">Done</span>' : '<span class="label label-important">New!!!</span>';
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        if (isset($_GET['ct_email']) AND $_GET['ct_email'] != '')
        {

            $criteria->compare('email', $_GET['ct_email'], true, 'AND', true);
        }
        if (isset($_GET['ct_phone']) AND $_GET['ct_phone'] != '')
        {

            $criteria->compare('phone', $_GET['ct_phone'], TRUE, 'AND', TRUE);
        }

        if (isset($_GET['ct_title']) AND $_GET['ct_title'] != '')
        {

            $criteria->compare('title', $_GET['ct_title'], TRUE, 'AND', TRUE);
        }

//        $criteria->compare('email', $this->email, true);
//        $criteria->compare('phone', $this->phone, true);
//        $criteria->compare('title', $this->title, true);
        $criteria->compare('message', $this->message, true);
        $criteria->compare('date_created', $this->date_created);
        //$criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort'     => array(
                'defaultOrder' => array(
                    'id' => CSort::SORT_DESC,
                )
            )
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Contact the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

}
